import 'dart:convert';
import 'dart:io';

import 'package:device_apps/device_apps.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_shensu/common/model/game.dart';
import 'package:flutter_shensu/common/style/AiSuColors.dart';
import 'package:imei_plugin/imei_plugin.dart';
import 'package:device_info/device_info.dart';
import 'package:connectivity/connectivity.dart';

class ISpeedPage extends StatefulWidget {
  static final String sName = "ispeed";

  final Game game;

  ISpeedPage(this.game);

  @override
  State<StatefulWidget> createState() {
    return _ISpeedPageState(this.game);
  }
}

class _ISpeedPageState extends State<StatefulWidget> {
  static const platform = const MethodChannel("com.aisu.ispeed/android");

  final Game game;

  _ISpeedPageState(this.game);

  String strISpeedState = "当前未加速";

  @override
  void initState() {
    super.initState();
    _getISpeedState();
  }

  @override
  Widget build(BuildContext context) {
    // 获取加速状态
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Color(AiSuColors.primaryAiSuValue),
        title: Text(this.game.game_name_zh),
      ),
      body: testWidget(),
    );
  }

  Widget testWidget() {
    return Center(
      child: new Center(
        child: Column(
          children: <Widget>[
            Text(strISpeedState),
            RaisedButton(
              onPressed: () {
                _startISpeed();
              },
              child: Text('加速'),
            ),
            RaisedButton(
              onPressed: () {
                _stopISpeed();
              },
              child: Text('停止加速'),
            ),
            RaisedButton(
              onPressed: () {
                DeviceApps.openApp(game.app_name);
              },
              child: Text('跳转到app'),
            )
          ],
        ),
      ),
    );
  }

  _startISpeed() async {
    // 调用原生安卓进行加速
    if (Platform.isAndroid) {
      try {
        String imei = await ImeiPlugin.getImei;

        DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
        AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
        String brand = androidInfo.brand;
        String model = androidInfo.model;
        String product = androidInfo.product;

        // ----------------------
        String networkType = "";

        var connectivityResult = await (Connectivity().checkConnectivity());
        if (connectivityResult == ConnectivityResult.mobile) {
          networkType = "moble";
        } else if (connectivityResult == ConnectivityResult.wifi) {
          networkType = "wifi";
        }

        Map<String, dynamic> jsonMap = {
          'app_name': game.app_name,
          'dispatch_type': game.dispatch_type,
          'routeID': game.routeID,
          'game_id': game.game_id,
          'imei': imei,
          'systemVersion': brand + "_" + model + "_" + product,
          'networkType': networkType,
        };

        String encodeGame = json.encode(jsonMap);
        final bool iSpeedState =
            await platform.invokeMethod("startISpeed", encodeGame);

        setState(() {
          if (iSpeedState) {
            strISpeedState = "当前正在加速";
          } else {
            strISpeedState = "当前未加速";
          }
        });
        print("加速状态: $iSpeedState");
      } on PlatformException catch (e) {
        print(e.toString());
      }
    }
  }

  _getISpeedState() async {
    // 调用原生安卓进行加速
    if (Platform.isAndroid) {
      try {
        final bool iSpeedState =
            await platform.invokeMethod("getISpeedState", "get_ispeed_state");
        setState(() {
          if (iSpeedState) {
            strISpeedState = "当前正在加速";
          } else {
            strISpeedState = "当前未加速";
          }
        });
      } on PlatformException catch (e) {
        print(e.toString());
      }
    }
  }

  _stopISpeed() async {
    // 调用原生安卓进行停止加速
    if (Platform.isAndroid) {
      try {
        final bool iSpeedState =
            await platform.invokeMethod("stopISpeed", "stop_ispeed");
        setState(() {
          if (iSpeedState) {
            strISpeedState = "当前正在加速";
          } else {
            strISpeedState = "当前未加速";
          }
        });
      } on PlatformException catch (e) {
        print(e.toString());
      }
    }
  }
}
